<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 2017/9/5
 * Time: 16:28
 */

namespace app\admin\model;


class UserModel extends BaseModel
{

    protected $table = "app_admin_user";
    protected $pk = "id";


    public function findUser($name, $pwd) {
        $where = array(
            'user_name' => $name,
            'is_del' => parent::NORMAL_STATUS,

        );

        $result = $this->where($where)->find();


        return $result;
    }

    /**
     * 根据id更新用户登录时间
     *
     * @param unknown $id
     */
    public function updateLoginTime($id) {
        $where = array(
            'id' => $id,
        );
        $saveData = array(
            'last_login_time' => date("Y-m-d H:i:s",time()),
            'login_count'     => ((int)session('user_info')['login_count'] + 1),
            'last_login_ip'   => request()->ip(),
        );
        return $this->where($where)->update($saveData);
    }

    /**
     * @description:每页显示数目
     *
     * @param unknown $num
     * @return multitype:unknown string
     */
    public function selectAllUser($num = 10) {
        $where = array(
            'status' => parent::NORMAL_STATUS,
        );

        $count = $this->where($where)->count();

        $list = $this->where($where)->paginate(parent::PAGE_LIMIT);
        $page = $list->render();

        return array(
            'page' => $page,
            'list' => $list,
            'count' => (int)($count / parent::PAGE_LIMIT) + 1);

    }

    /**
     * @description:添加后台用户
     *
     * @param unknown $data
     * @return boolean
     */
    public function addAdminUser($data) {

        return $this->insert($data) ? true : false;
    }

    /**
     * @description:更新用户信息
     *
     * @param unknown $data
     */
    public function editAdminUser($data) {
        $where = array(
            'id' => $data['id'],
        );

        unset($data['id']);
        return $this->where($where)->Update($data);
    }

    /**
     * @description:删除用户
     *
     * @param unknown $user_id
     * @return Ambigous <boolean, unknown>
     */
    public function deleteAdminUser($user_id) {
        $where = array(
            'id' => $user_id,
        );

        $data = array(
            'status' => parent::DEL_STATUS,
        );

        return $this->where($where)->update($data);
    }


    /**
     * @description:根据id查询用户
     *
     * @param unknown $user_id
     */
    public function findAdminUserById($user_id) {
        $where = array(
            'id' => $user_id,
            'status' => parent::NORMAL_STATUS,
        );

        return $this->where($where)->find();
    }

    public function findAdminUserByName($user_name) {
        $where = array(
            'user_name' => $user_name,
            'status' => parent::NORMAL_STATUS,
        );


        return $this->where($where)->find();
    }

    public function findUserAuth($user_id) {


    }

}